home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / AllocRing.cc < prev    next >
C/C++ Source or Header  |  1992-05-02  |  2KB  |  111 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include <std.h>
  23. #include <AllocRing.h>
  24. #include <new.h>
  25.  
  26. AllocRing::AllocRing(int max)
  27.   :n(max), current(0), nodes(new AllocQNode[max])
  28. {
  29.   for (int i = 0; i < n; ++i)
  30.   {
  31.     nodes[i].ptr = 0;
  32.     nodes[i].sz = 0;
  33.   }
  34. }
  35.  
  36. int AllocRing::find(void* p)
  37. {
  38.   if (p == 0) return -1;
  39.  
  40.   for (int i = 0; i < n; ++i)
  41.     if (nodes[i].ptr == p)
  42.       return i;
  43.  
  44.   return -1;
  45. }
  46.  
  47.  
  48. void AllocRing::clear()
  49. {
  50.   for (int i = 0; i < n; ++i)
  51.   {
  52.     if (nodes[i].ptr != 0)
  53.     {
  54.       delete(nodes[i].ptr);
  55.       nodes[i].ptr = 0;
  56.     }
  57.     nodes[i].sz = 0;
  58.   }
  59.   current = 0;
  60. }
  61.  
  62.  
  63. void AllocRing::free(void* p)
  64. {
  65.   int idx = find(p);
  66.   if (idx >= 0)
  67.   {
  68.     delete nodes[idx].ptr;
  69.     nodes[idx].ptr = 0;
  70.   }
  71. }
  72.  
  73. AllocRing::~AllocRing()
  74. {
  75.   clear();
  76. }
  77.  
  78. int AllocRing::contains(void* p)
  79. {
  80.   return find(p) >= 0;
  81. }
  82.  
  83. static inline unsigned int good_size(unsigned int s)
  84. {
  85.   unsigned int req = s + 4;
  86.   unsigned int good = 8;
  87.   while (good < req) good <<= 1;
  88.   return good - 4;
  89. }
  90.  
  91. void* AllocRing::alloc(int s)
  92. {
  93.   unsigned int size = good_size(s);
  94.  
  95.   void* p;
  96.   if (nodes[current].ptr != 0 && 
  97.       nodes[current].sz >= int(size) && 
  98.       nodes[current].sz < int(4 * size))
  99.     p = nodes[current].ptr;
  100.   else
  101.   {
  102.     if (nodes[current].ptr != 0) delete nodes[current].ptr;
  103.     p = new char[size];
  104.     nodes[current].ptr = p;
  105.     nodes[current].sz = size;
  106.   }
  107.   ++current;
  108.   if (current >= n) current = 0;
  109.   return p;
  110. }
  111.